/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.youxin.infra.task;

import com.youxin.infra.entity.ItemSink;
import com.youxin.infra.io.ProducerStringSerializationSchema;
import com.youxin.infra.kafkaUtil.KafkaProperties;
import com.youxin.infra.sink.KafkaSink;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer;
import org.apache.flink.table.api.EnvironmentSettings;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.types.Row;

import java.util.Properties;

import static com.youxin.infra.util.ConstantsUtil.*;
import static org.apache.flink.table.api.Expressions.$;

/**
 * Simple example for demonstrating the use of SQL in Java.
 *
 * @author sunteng
 */
public class StreamWindowSQLExample {

    private static final TypeInformation<Row> SCHEMA = TypeInformation.of(Row.class);

    public static void main(String[] args) throws Exception {
        // set up execution environment
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        EnvironmentSettings settings = EnvironmentSettings.newInstance().inStreamingMode().useBlinkPlanner().build();
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env, settings);

        //create table op
        tableEnv.executeSql(KAFKA_SQL_EVEN_CREATE);
        tableEnv.executeSql(KAFKA_SQL_ODD_CREATE);

        // windows count op
        Table resultEven = tableEnv.sqlQuery(QUERY_EVEN_AGG_SQL);
        resultEven.printSchema();

        Table resultOdd = tableEnv.sqlQuery(QUERY_ODD_AGG_SQL);
        resultOdd.printSchema();

        //sql join op
        Table joinTable = resultEven.join(resultOdd)
                .where(
                        $("even_name").isEqual($("odd_name")))
                .select(
                        $("even_id").as("id"),
                        $("even_name").as("name"),
                        $("even_cnt").as("counts"),
                        $("ts"));

        joinTable.printSchema();

        //sink to kafka 1
        DataStream<ItemSink> sinkResultStream = tableEnv.toAppendStream(joinTable, ItemSink.class);

        //kafka connector
        Properties props = new Properties();
        props.put("bootstrap.servers", KafkaProperties.KAFKA_SERVER_URL + ":" + KafkaProperties.KAFKA_SERVER_PORT);
        props.put("client.id", KafkaProperties.CLIENT_ID);

        FlinkKafkaProducer<ItemSink> myProducer = new FlinkKafkaProducer<>(
                KafkaProperties.TOPIC_SINK,
                new ProducerStringSerializationSchema(KafkaProperties.TOPIC_SINK),
                props,
                FlinkKafkaProducer.Semantic.EXACTLY_ONCE);

        sinkResultStream.addSink(myProducer);

        //run
        env.execute("Streaming Window SQL Job");

    }
}
